package dk.simonvogensen.itodo.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;

public class GWTUtils implements ITodoGWTUtils {

	private static final int UPDATE_TODO_DELAY = 5000;
	
	private Timer updateTimer;

	public void initUpdateTimer(final Command callback) {
		updateTimer = new Timer() {
			public void run() {
				callback.execute();
			}
		};
	}
	
	public void refreshUpdateTimer() {
		updateTimer.schedule(UPDATE_TODO_DELAY);
	}
	
	public void cancelUpdateTimer() {
    	updateTimer.cancel();
	}

    private static Map metaPropertiesMap = null;
    
	public String getMetaProperty(String name) {
        if (metaPropertiesMap == null) {
        	metaPropertiesMap = getMetaProperties(new HashMap());
        }
        return (String) metaPropertiesMap.get(name);
    }
	
    private static native Map<String, String> getMetaProperties(Map<String, String> map) /*-{
        var metas = $wnd.document.getElementsByTagName("meta");
        for (var i = 0, n = metas.length; i < n; ++i) {
            var meta = metas[i];
            var name = meta.getAttribute("name");
            if (name && name == "gwt:property") {
                var content = meta.getAttribute("content");
                if (content) {
                    var name = content, value = "";
                    var eq = content.indexOf("=");
                    if (eq != -1) {
                        name  = content.substring(0, eq);
                        value = content.substring(eq+1);
                    }
                    map.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)(name,value);
                }
            }
        }
        return map;

    }-*/;
}
